﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using HDVN5.HoTro;
namespace HDVN5.WebServices
{
    /// <summary>
    /// Summary description for CacDanhMuc
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class CacDanhMuc : System.Web.Services.WebService
    {
        private DanhMucDataContext db = new DanhMucDataContext();
        private LoginModule.AccountModuleDataContext dbAcc = new LoginModule.AccountModuleDataContext();
        [WebMethod]
        [System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
        public DANHMUCTINHTHANH[] GetTinhThanh()
        {
            return db.DANHMUCTINHTHANHs.ToArray();
        }
        [WebMethod]
        [System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
        public DANHMUCNGANH[] getDanhMucNganh()
        {
            return db.DANHMUCNGANHs.ToArray();
        }
        [WebMethod]
        [System.Web.Script.Services.ScriptMethod(UseHttpGet = false, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
        public SOQUANLYDONVI[] getLienDoan(string maId)
        {
            return db.SOQUANLYDONVIs.Where(p => p.MACAPTRUCTHUOC.ToString() == maId).ToArray();
        }

        [WebMethod]
        public IList<string> getTinhThanh(string tem)
        {
            var qu = from p in db.DANHMUCTINHTHANHs where p.TENTINH.ToLower().Contains(tem.ToLower()) select p;
            IList<string> lst = new List<string>();
            foreach (var item in qu.ToList())
            {
                lst.Add(item.TENTINH);
            }
            return lst;
        }
        /// <summary>
        /// Thay doi thong tin don vi
        /// </summary>
        /// <param name="maDonVi"></param>
        /// <param name="tenDonVi"></param>
        /// <param name="email"></param>
        /// <param name="tenTruong"></param>
        /// <returns></returns>
        [WebMethod]
 
        public SOQUANLYDONVI[] getLienDoanA(string tinhThanh)
        {
            var qLienDoanThuocTinh = from liendoan in db.SOQUANLYDONVIs
                                     where liendoan.TINHTHANH == Convert.ToInt32(tinhThanh) && (liendoan.CAPID==2 || liendoan.CAPID == 4)
                                     select liendoan;

            return qLienDoanThuocTinh.ToArray();

        }

        [WebMethod]
        
        public SOQUANLYDONVI[] getDonViNganh(string maDonViCapTrucThuoc)
        {
            var qLienDoanThuocTinh = from liendoan in db.SOQUANLYDONVIs
                                     where liendoan.MACAPTRUCTHUOC == Convert.ToInt32(maDonViCapTrucThuoc)
                                     select liendoan;

            return qLienDoanThuocTinh.ToArray();
        }

        [WebMethod]
        public string getThongKe(string MaDonVi)
        {
            Int32 ma = Convert.ToInt32(MaDonVi);
            RenLuyenModule.ChuongTrinhDangThu thongKeClass = new RenLuyenModule.ChuongTrinhDangThu(ma);
            RenLuyenModule.CustomModel.ThongKeSummary summary = thongKeClass.summaryReportLevelProgram();

            return HoTro.TaoJSON.ToJSON(summary);
        }
    }
}
